package com.netty.demo;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Auther: Admin
 * @Date: 2021/4/13 15:50
 * @Description:
 */
@Slf4j
public class TestByteBuffer {

    public static void main(String[] args) {


        try (FileChannel channel = new FileInputStream("data.txt").getChannel()) {// twr语法自动关闭


            ByteBuffer buffer = ByteBuffer.allocate(10);

            while (true){


                int len = channel.read(buffer);
                log.info("读取字节数{}", len);
                if(len == -1){
                    break;
                }

                buffer.flip();// 切换为读模式
                while (buffer.hasRemaining()){
                    log.info("字节内容{}", (char) buffer.get() );
                }


                buffer.clear();// 切换为写模式
//                buffer.compact();// 切换写模式
            }



        }catch (Exception e){


        }


    }

}
